package com.woniuxy.stage3demo.controller.chenyuxuanController;

import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.StaffBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.chenyxuanService.IStaffService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 员工cnntroller
 */
@RestController
@RequestMapping("staff")
public class StaffController {

    @Resource
    private IStaffService staffServiceImpl;

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg deleteStaff(@PathVariable("id") int id){
        System.out.println("这是删除部门controller"+id);
        ResultMsg res = new ResultMsg(200,"操作成功",null);
      int row=  staffServiceImpl.deleteStaff(id);
        if(row!=1 ) {
            staffServiceImpl.deleteStaff(id);
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }

    /**
     * 员工分页查询
     * @param pageRequest
     * @param staffBean
     * @return
     */
    @GetMapping(value ="/page",produces = {"application/json;charset=utf-8"})
    public TableResult<StaffBean> findAllStaff(PageRequest pageRequest,StaffBean staffBean){
       // System.out.println(pageRequest);
        System.out.println(staffBean);
        PageInfo<StaffBean> pageInfo=staffServiceImpl.findAllByParams(pageRequest,staffBean);
        System.out.println(pageInfo);
        return  new TableResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 修改员工信息
     * @param staffBean
     * @return
     */
    @PutMapping(value = "/update",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateStaff(@RequestBody StaffBean staffBean){
        staffBean.setCreateTime(new Date());
        staffBean.setDelFlag(0);
        System.out.println(staffBean);
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        int row=staffServiceImpl.updateStaff(staffBean);
        if(row == 1){
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }

    /**
     * 修改员工使用状态码
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/status/{id}/{status}", produces = {"application/json;charset=utf-8"})
    public ResultMsg statusUpdate(@PathVariable("id") Long id, @PathVariable("status") long status) {
        ResultMsg res = new ResultMsg(200, "操作成功", null);
        System.out.println(id);
        System.out.println(status);
        StaffBean staffBean=new StaffBean();
        staffBean.setId(id);
        staffBean.setUseStatus(status);
        int row=  staffServiceImpl.updateStaff(staffBean);
        if ( row==1) {
            return res;
        } else {
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }

    /**
     * 添加员工信息
     * @param staffBean
     * @return
     */
    @PostMapping(value = "/add", produces = {"application/json;charset=utf-8"})
    public ResultMsg addStaff(@RequestBody StaffBean staffBean) {
        System.out.println(staffBean+"这是添加员工信息");
        staffBean.setDelFlag(0);
        Date date = new Date();
        staffBean.setLastLoginTime(date);
        staffBean.setCreateTime(date);
        ResultMsg resultMsg = new ResultMsg(200, "新增成功", null);
        System.out.println(staffBean);
        staffServiceImpl.addStaffInDept(staffBean);
        return resultMsg;
    }
}
